<template>
  <a-drawer
    :title="title"
    :maskClosable="true"
    :width="drawerWidth"
    placement="right"
    :closable="true"
    @close="handleCancel"
    :visible="visible"
    style="height: 100%;">
    <template slot="title">
      <div style="width: 100%;">
        <span>{{ title }}</span>
        <span style="display:inline-block;width:calc(100% - 51px);padding-right:10px;text-align: right">
          <a-button @click="toggleScreen" icon="appstore" style="height:20px;width:20px;border:0px"></a-button>
        </span>
      </div>
    </template>
    <a-spin :spinning="confirmLoading">

      <a-form-model ref="form" :model="model" :rules="validatorRules">
        <a-form-model-item
          label="歌曲名字"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          prop="songName"
        >
          <a-input placeholder="请输入歌曲名字" v-model="model.songName" />
        </a-form-model-item>
        <a-form-model-item
          label="所属歌手"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          prop="artistId"
        >
          <j-dict-select-tag v-model="model.artistId" placeholder="请选择歌手" dictCode="mus_artist,cname,id"/>
        </a-form-model-item>
        <a-form-model-item
          label="选择专辑"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          prop="albumId"
        >
          <j-select-albums v-model="model.albumId" :buttons="true" placeholder="请选择专辑" :multiple="false"/>
        </a-form-model-item>
        <a-form-model-item
          label="出品公司"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          prop="publisher"
        >
          <a-input placeholder="唱片公司" v-model="model.publisher" />
        </a-form-model-item>
        <a-form-model-item
          label="选择流派"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          prop="genreId"
        >
          <j-category-select v-model="model.genreId" pcode="B03A01" :multiple="false"/>
        </a-form-model-item>
        <a-form-model-item
          label="选择语种"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          prop="languageId"
        >
          <j-category-select v-model="model.languageId" pcode="B03A02" :multiple="false"/>
        </a-form-model-item>
        <a-form-model-item
          label="基本介绍"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          prop="description"
        >
          <a-textarea :rows="4" v-model="model.description" placeholder="请输入描述"/>
        </a-form-model-item>
        <a-form-model-item
          label="热点标志"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          prop="alias"
        >
          <a-input placeholder="热度说明" v-model="model.alias" ></a-input>
        </a-form-model-item>
        <a-form-model-item
          label="发布日期"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          prop="publishDate"
        >
          <a-date-picker
            style="width: 100%"
            placeholder="请选择时间"
            v-model="model.publishDate"
            :format="dateFormat"
            :getCalendarContainer="node => node.parentNode"/>
        </a-form-model-item>
        <a-form-model-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          prop="duration"
          label="MP3时长">
          <a-input-number v-model="model.duration"/>
        </a-form-model-item>
        <a-form-model-item
          label="作曲"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          prop="composer"
        >
          <a-input placeholder="谱曲人" v-model="model.composer" />
        </a-form-model-item>
        <a-form-model-item
          label="作词"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          prop="lyricist"
        >
          <a-input placeholder="写词人" v-model="model.lyricist" />
        </a-form-model-item>
        <a-form-model-item
          label="图片上传"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          prop="avatar"
        >
          <j-image-upload bizPath="scott/pic" v-model="model.avatar"></j-image-upload>
        </a-form-model-item>
        <a-form-model-item
          label="MP3音频"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          prop="audioFile"
        >
          <j-upload v-model="model.audioFile"></j-upload>
        </a-form-model-item>
        <a-form-model-item
          label="歌词信息"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          prop="lyric"
        >
          <a-textarea :rows="7" v-model="model.lyric" placeholder="请输入歌词"/>
        </a-form-model-item>
      </a-form-model>
    </a-spin>
    <div class="drawer-bootom-button" v-show="!disableSubmit">
      <a-popconfirm title="确定放弃编辑？" @confirm="handleCancel" okText="确定" cancelText="取消">
        <a-button style="margin-right: .8rem">取消</a-button>
      </a-popconfirm>
      <a-button type="primary" :loading="confirmLoading" @click="handleSubmit">提交</a-button>
    </div>
  </a-drawer>
</template>

<script>
  import JUpload from '@/components/jeecg/JUpload'
  import moment from 'moment'
  import JImageUpload from '@/components/jeecg/JImageUpload'
  import JCategorySelect from '@comp/jeecg/JCategorySelect'
  import JDictSelectTag from '@/components/dict/JDictSelectTag.vue'
  import JSelectAlbums from '../../tools/JSelectAlbums'
  import Vue from 'vue'
  import { ACCESS_TOKEN } from "@/store/mutation-types"
  import { getAction } from '@/api/manage'
  import { addSong,editSong } from '@/api/api'
  import Result from '../../../result/Result'
  export default {
    name: 'SongModel',
    components: { Result, JDictSelectTag,JCategorySelect,JUpload,JImageUpload,JSelectAlbums},
    data () {
      return {
        songIdShow:false,
        title:"操作",
        visible: false,

        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        uploadLoading:false,
        confirmLoading: false,
        headers:{},
        departDisabled: false, //是否是我的部门调用该页面
        roleDisabled: false, //是否是角色维护调用该页面
        modalWidth:800,
        drawerWidth:700,
        modaltoggleFlag:true,
        confirmDirty: false,
        userId:"", //保存用户id
        disableSubmit:false,
        description: '将一个冗长或用户不熟悉的表单任务分成多个步骤，指导用户完成。',
        currentTab: 0,
        loading: false,
        // form
        form: null,
        songId: "",
        model: {},
        dateFormat:"YYYY-MM-DD",
        validatorRules:{
          songName:[{ required: true, message: '请输入歌曲名字！' }],
          description:[{required: true, message: '请输入简介内容!'},
            {validator: this.validateUsername,}],
          artistId: [{ required: true, message: '请选择歌手！' }] ,
          languageId: [{ required: true, message: '语种必填！' }] ,
          avatar:[{ required: true, message: '封面图片必须上传！' }],
          audioFile:[{ required: true, message: '音频文件必须上传！' }],
        },
        url: {
          fileUpload: window._CONFIG['domianURL']+"/sys/common/upload",
          songId: "/sys/user/generateUserId",
        },
      }
    },
    methods: {
      add () {
        this.refresh();
        this.edit({activitiSync:'1',songIdentity:1});
      },
      edit (record) {
        let that = this;
        that.visible = true;
        //根据屏幕宽度自适应抽屉宽度
        this.resetScreenSize();
        that.songId = record.id;
        that.model = Object.assign(record);
        console.log('that.model=',that.model)
      },
      moment,
      handleCancel () {
        this.close()
      },
      //窗口最大化切换
      toggleScreen(){
        if(this.modaltoggleFlag){
          this.modalWidth = window.innerWidth;
        }else{
          this.modalWidth = 800;
        }
        this.modaltoggleFlag = !this.modaltoggleFlag;
      },
      // 根据屏幕变化,设置抽屉尺寸
      resetScreenSize(){
        let screenWidth = document.body.clientWidth;
        if(screenWidth < 500){
          this.drawerWidth = screenWidth;
        }else{
          this.drawerWidth = 700;
        }
      },
      refresh () {
        this.songId=""
        this.nextDepartOptions=[];
        this.songIdShow=false;
      },
      close () {
        this.$emit('close');
        this.visible = false;
        this.disableSubmit = false;
        this.songIdShow=false;
        this.$refs.form.resetFields();
      },
      handleSubmit () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let obj;
            if(!this.model.id){
              this.model.id = this.songId;
            }else{
              obj=editSong(this.model);
            }
            obj.then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })
          }else{
            return false;
          }
        })
      },
    }
  }
</script>

<style scoped>
  .avatar-uploader > .ant-upload {
    width:104px;
    height:104px;
  }
  .ant-upload-select-picture-card i {
    font-size: 49px;
    color: #999;
  }

  .ant-upload-select-picture-card .ant-upload-text {
    margin-top: 8px;
    color: #666;
  }

  .ant-table-tbody .ant-table-row td{
    padding-top:10px;
    padding-bottom:10px;
  }

  .drawer-bootom-button {
    position: absolute;
    bottom: 0;
    width: 100%;
    border-top: 1px solid #e8e8e8;
    padding: 10px 16px;
    text-align: right;
    left: 0;
    background: #fff;
    border-radius: 0 0 2px 2px;
  }

  /*【JTC-502】 添加用户两个滚动条*/
  /deep/ .ant-drawer-body {
    padding-bottom: 53px;
  }
</style>